package com.martin.lib_base.widget

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import com.martin.lib_base.R

class RadiusTextView : AppCompatTextView {

    //圆角角度
    private var radius = 0f

    //是否是圆形
    private var isRound = false

    //边框画笔
    private var borderPaint: Paint? = null

    //边框宽度
    private var borderWidth = 0f

    //背景画笔
    private var bgPaint: Paint? = null

    constructor(context: Context) : super(context) {
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init(context, attrs)
    }

    /**
     * 初始化
     */
    private fun init(context: Context, attrs: AttributeSet?) {
        val attributes = context.obtainStyledAttributes(attrs, R.styleable.RadiusTextView)
        //获取圆角信息
        radius =
            attributes.getDimension(R.styleable.RadiusTextView_rtv_radius, 0f)
        isRound = attributes.getBoolean(R.styleable.RadiusTextView_rtv_is_round, false)
        //背景色
        val bgColor =
            attributes.getColor(R.styleable.RadiusTextView_rtv_bg_color, Color.TRANSPARENT)

        //获取边框信息
        borderWidth =
            attributes.getDimension(R.styleable.RadiusTextView_rtv_border_width, 0f)
        val borderColor =
            attributes.getColor(R.styleable.RadiusTextView_rtv_border_color, Color.TRANSPARENT)

        //获取虚线信息
        val dashWidth =
            attributes.getDimension(R.styleable.RadiusTextView_rtv_dash_width, 0f)
        val dashSpace =
            attributes.getDimension(R.styleable.RadiusTextView_rtv_dash_space, 0f)

        //创建边框画笔
        if (borderWidth > 0) {
            borderPaint = Paint().apply {
                isAntiAlias = true
                style = Paint.Style.STROKE
                color = borderColor
                strokeWidth = borderWidth
            }
            //虚线信息都不为0再处理
            if (dashWidth.coerceAtLeast(dashSpace) != 0f) {
                borderPaint?.pathEffect = DashPathEffect(floatArrayOf(dashWidth,dashSpace),1f)
            }
        }

        //创建背景色画笔
        if (bgColor != Color.TRANSPARENT) {
            bgPaint = Paint().apply {
                isAntiAlias = true
                style = Paint.Style.FILL
                color = bgColor
                strokeWidth = borderWidth
            }
        }

        //回收
        attributes.recycle()
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        //计算圆角大小
        val minSize = width.coerceAtMost(height).toFloat()
        radius = if (isRound) minSize else radius.coerceAtMost(minSize)

        val halfBorderWidth = borderWidth / 2
        val rectF = RectF(
            halfBorderWidth,
            halfBorderWidth,
            width.toFloat() - halfBorderWidth,
            height.toFloat() - halfBorderWidth
        )
        //绘制背景色
        bgPaint?.let {
            //获取矩形
            canvas.drawRoundRect(rectF, radius, radius, it)
        }

        super.onDraw(canvas)

        //绘制边框
        borderPaint?.let {
            canvas.drawRoundRect(rectF, radius, radius, it)
        }
    }


}